//
//  MyTabbarViewController.swift
//  Pregnancy
//
//  Created by android on 2019/9/11.
//  Copyright © 2019 ios. All rights reserved.
//

import UIKit
import CYLTabBarController

public var MainViewController = MyTabbarViewController.shared

public class MyTabbarViewController: CYLTabBarController{


    public static let shared = MyTabbarViewController(viewControllers: MyTabbarViewController.setNavViewControllers(), tabBarItemsAttributes: MyTabbarViewController.tabBarItemsAttributesForController())
    public static var controllers: [UIViewController] = []
    
    public override func viewDidLoad() {

        super.viewDidLoad()
        self.delegate = self
        commitInitView()
        if #available(iOS 15.0, *) {
            let appearance  = UITabBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = UIColor.hexColor("#001310")
            ///Resolved that the selected color cannot be modified
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [.font:UIFont.pingfangSC_Semibold(fontSize: 13.0),.foregroundColor: UIColor.hexColor("#80FFE6")]
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [.font:UIFont.pingfangSC_Regular(fontSize: 13.0),.foregroundColor: UIColor.hexColor("#001310")]
            self.tabBar.standardAppearance = appearance
            self.tabBar.scrollEdgeAppearance = appearance
        } else {
            // Fallback on earlier versions
            UITabBar.appearance().barTintColor = UIColor.hexColor("#001310")
        }
        
        self.tabBar.tintColor = UIColor.hexColor("80FFE6")
        self.tabBar.unselectedItemTintColor = UIColor.clear
        
    }
    
    static func setNavViewControllers() -> [XGLNavigationController]{
        self.controllers = [XGL_HomeViewController(),XGL_LiveViewController(),XGL_MessageViewController(),XGL_PersonalViewController()]
        let naviDatas = self.controllers.map { controller in
            let tabbar2 = XGLNavigationController(rootViewController: controller)
            tabbar2.tabBarItem.setTitleTextAttributes([.font:UIFont.pingfangSC_Regular(fontSize: 13.0),.foregroundColor: UIColor.hexColor("#001310")], for: .normal)
            tabbar2.tabBarItem.setTitleTextAttributes([.font:UIFont.pingfangSC_Semibold(fontSize: 13.0),.foregroundColor: UIColor.hexColor("#80FFE6")], for: .selected)
            return tabbar2
        }
       
        return naviDatas
        
    }
    static func tabBarItemsAttributesForController() ->  [[String : Any]] {
        
        let tabBarItemOne: [String: Any] = [CYLTabBarItemTitle:"•",
                             CYLTabBarItemImage: imageWithName("img_xgl_tabbar_home_normal").withRenderingMode(.alwaysOriginal) ,
                                    CYLTabBarItemSelectedImage: imageWithName("img_xgl_tabbar_home_selected").withRenderingMode(.alwaysOriginal)]
        
        let tabBarItemTwo: [String: Any] = [CYLTabBarItemTitle:"•",
                             CYLTabBarItemImage: imageWithName("img_xgl_tabbar_live_normal").withRenderingMode(.alwaysOriginal),
                                    CYLTabBarItemSelectedImage: imageWithName("img_xgl_tabbar_live_selected").withRenderingMode(.alwaysOriginal)]

        let tabBarItemThree: [String: Any] = [CYLTabBarItemTitle:"•",
                               CYLTabBarItemImage: imageWithName("img_xgl_tabbar_message_normal").withRenderingMode(.alwaysOriginal),
                               CYLTabBarItemSelectedImage: imageWithName("img_xgl_tabbar_message_selected").withRenderingMode(.alwaysOriginal)]
        let tabBarItemFour: [String: Any] = [CYLTabBarItemTitle:"•",
                              CYLTabBarItemImage: imageWithName("img_xgl_tabbar_personal_normal").withRenderingMode(.alwaysOriginal),
                              CYLTabBarItemSelectedImage: imageWithName("img_xgl_tabbar_personal_selected").withRenderingMode(.alwaysOriginal)]
        let tabBarItemsAttributes = [tabBarItemOne,tabBarItemTwo,tabBarItemThree,tabBarItemFour]
        return tabBarItemsAttributes
    }
    
    func addScaleAnimationOnView(animationView:UIView,repeatCount:Float) {
        let animation = CAKeyframeAnimation()
        animation.keyPath = "transform.scale"
        animation.values = [1.0,1.3,0.9,1.15,0.95,1.02,1.0]
        animation.duration = 1
        animation.repeatCount = repeatCount
        animation.calculationMode = .cubic
        animationView.layer.add(animation, forKey: nil)
    }

}
extension MyTabbarViewController {
    func commitInitView() {
        view.backgroundColor = UIColor.hexColor("#001310")
        tabBar.isTranslucent = false
        tabBar.barTintColor = UIColor.hexColor("#001310")
        tabBar.layer.cornerRadius = 24.0
        
        //tabbar
//        let imageview = UIImageView(image: #imageLiteral(resourceName: "img_xgl_tabbar_back"))
//        self.tabBar.insertSubview(imageview, at: 0)
//        let contentView = MXTabbarBackContentView()
//        contentView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: self.tabBar.height)
//        UITabBar.appearance().backgroundImage = contentView.getImage()
    }

    
    public override func tabBarController(_ tabBarController: UITabBarController, didSelect control: UIControl) {
        
        if control.cyl_isPlusButton() {
            let button = CYLExternPlusButton
            let animationView = (button.imageView)!
            self.addScaleAnimationOnView(animationView: animationView, repeatCount: 1)
        }else{
            let animationView = control.cyl_tabImageView()
            self.addScaleAnimationOnView(animationView: animationView, repeatCount: 1)
        }
    }
}
